#!/usr/bin/perl -w
use strict;
use utf8;
use AnyEvent;
use Net::XMPP2::Client;
use Net::XMPP2::Ext::Registration;
use Net::XMPP2::Util qw/split_jid/;

sub result {
   my ($reg, $ok, $error, $form, $comment) = @_;
   $comment ||= 'REGISTERED!';
   if ($ok) {
      print "$comment\n";
   } else {
      print "ERROR: " . $error->string . " [form: $form]\n";
   }
}

my ($user, $pw, $act) = @ARGV;
my ($username, $server) = split_jid ($user);

unless ($user && $pw) {
print <<USAGE;
usage:
  register account:
     jabber-tool-account-register <jid> <password>

  unregister account:
     jabber-tool-account-register <jid> <password> unregister

  change password:
     jabber-tool-account-register <jid> <password> chpw:<newpassword>

USAGE
die "\n";
}

my $j = AnyEvent->condvar;
my $cl = Net::XMPP2::Client->new (debug => 1);
$cl->add_account ($user, $pw);
$cl->reg_cb (
   stream_pre_authentication => sub {
      my ($cl, $acc) = @_;

      my $reg = Net::XMPP2::Ext::Registration->new (connection => $acc->connection);

      if ($act eq '') {
         $reg->send_registration_request (sub {
            my ($reg, $form, $error) = @_;

            if ($error) {
               print "ERROR: " . $error->string . "\n";

            } else {
               my $df = $form->get_data_form;
               my $lf = $form->get_legacy_form_fields;
               my $oo = $form->get_oob;

               if ($df) {
                  print "GOT FORM! ".($df ? $df->as_debug_string : "")."\n";
               }
               if ($lf) {
                  require Data::Dumper;
                  print "LEGACY: " . Data::Dumper->Dump ([$lf]) . "\n";
               }
               if ($oo) {
                  print "OOB: $oo->{url} / $oo->{desc}\n";
               }

               my $af = $form->try_fillout_registration ($username, $pw);
               $reg->submit_form ($af, \&result);
            }
         });
         return 0
      }
      1
   },
   session_ready => sub {
      my ($cl, $acc) = @_;

      my $reg = Net::XMPP2::Ext::Registration->new (connection => $acc->connection);

      if ($act eq 'unregister') {
         $reg->send_unregistration_request (
            sub { result ($_[0], $_[1], $_[2], $_[3], 'UNREGISTERED!') }
         );

      } elsif ($act =~ /chpw:(\S+)/) {
         $reg->send_password_change_request ($username, $1,
            sub { result ($_[0], $_[1], $_[2], $_[3], 'CHANGED PW!') }
         );
      }
   },
   disconnect => sub {
      my ($cl, $acc, $h, $p, $reas) = @_;
      print "disconnect ($h:$p): $reas\n";
   },
   error => sub {
      my ($cl, $acc, $err) = @_;
      print "ERROR: " . $err->string . "\n";
   },
   message => sub {
      my ($cl, $acc, $msg) = @_;
      print "message from: " . $msg->from . ": " . $msg->any_body . "\n";
   }
);

$cl->start;
$j->wait;
